using System;
using System.Collections;
using System.Collections.Generic;
using Wintellect.PowerCollections;

namespace CS2.Core
{
    public class SynchronizedStringSet : ISynchronizedStringSet
    {
        mutable set : Set[string];
        syncLock = object();

        public this()
        {
            set = Set.[string](StringComparer.InvariantCultureIgnoreCase);
        }

        private this(inner : Set[string])
        {
            set = inner;
        }

        public Add(item : string) : bool
        {
            lock(syncLock)
                set.Add(item);
        }

        public Clear() : void
        {
            lock(syncLock)
                set.Clear();
        }

        public Count : int
        {
            get { set.Count; }
        }

        public CloneAndClear() : ISynchronizedStringSet
        {
            lock(syncLock)
            {
                def clone = SynchronizedStringSet(set.Clone());
                set.Clear();
                clone;
            }
        }

        public GetEnumerator() : IEnumerator[string]
        {
            set.GetEnumerator();
        }

        public Remove(item : string) : bool
        {
            lock(syncLock)
                set.Remove(item);
        }
    }
}